package com.example.day04.controller.richfunc;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class RichFunctionDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Integer> integerDataStreamSource = env.fromElements(1, 2, 3, 4);
        SingleOutputStreamOperator<Integer> map = integerDataStreamSource.map(new RichMapFunction<Integer, Integer>() {

            @Override
            public Integer map(Integer val) throws Exception {
                return val + 1;
            }

            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                System.out.println(
                        "子任务编号=" + getRuntimeContext().getIndexOfThisSubtask()
                                + ", 子任务名称=" + getRuntimeContext().getTaskNameWithSubtasks() + ", 调用open()");
            }


            @Override
            public void close() throws Exception {
                super.close();
                System.out.println(
                        "子任务编号=" + getRuntimeContext().getIndexOfThisSubtask()
                                + ", 子任务名称=" + getRuntimeContext().getTaskNameWithSubtasks() + ", 调用close()");
            }
        });
        map.print();

        env.execute();
    }
}
